/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdInfoRedes.java
 *
 * Created on 02/06/2010, 10:33:21 AM
 */

package gestionseguridadadministrativacliente;

import administracion.helper.Conversiones;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.Utiles_Tabla;
import administracion.propiedades.Propiedades;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.List;

/**
 *
 * @author Renato
 */
public class jdInfoRedes extends javax.swing.JDialog {

    /** Creates new form jdInfoRedes */
    public jdInfoRedes(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cargarValoresInterfaces();
    }

    private void formatearTabla(Object[][] val, String[] col, Class[] tipo, boolean[] edit) {
    final Class[] tipos = tipo;
    final boolean[] editable = edit;
    this.jtTablaRedes.setModel(new javax.swing.table.DefaultTableModel(val, col) {
      public Class getColumnClass(int columnIndex) {
        return tipos[columnIndex];
      }
      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return editable[columnIndex];
      }
    });
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtTablaRedes = new javax.swing.JTable();
        jbAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdInfoRedes.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel1.border.titleFont"), resourceMap.getColor("jPanel1.border.titleColor"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtTablaRedes.setFont(resourceMap.getFont("jtTablaRedes.font")); // NOI18N
        jtTablaRedes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Interface", "IP", "Host", "Address"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jtTablaRedes.setName("jtTablaRedes"); // NOI18N
        try{
            Utiles_Tabla.definirAnchoColumnasJTable(this.jtTablaRedes, new int[]{35,15,15,35});
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        jScrollPane1.setViewportView(jtTablaRedes);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 499, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 153, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE))
        );

        jbAceptar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/apply.png"))); // NOI18N
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });

        jbCancelar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setText(resourceMap.getString("jbCancelar.text")); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(289, Short.MAX_VALUE)
                .addComponent(jbAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCancelar)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbAceptar, jbCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCancelar)
                    .addComponent(jbAceptar))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        
        try {
      byte[] direcc = Conversiones.convierteHexadecimalString_ArrayByte(this.jtTablaRedes.getValueAt(this.jtTablaRedes.getSelectedRow(), 3).toString());
      String host = this.jtTablaRedes.getValueAt(this.jtTablaRedes.getSelectedRow(), 2).toString();
      Propiedades.estableceDireccion(host, direcc);
      Utiles_MensajesDialogo.mensajeInfo("Propiedades Establecidas con Exito");
    } catch (Exception e) {
      Utiles_MensajesDialogo.mensajeError("Error al obtener la direccion fisica\n" + e.toString());
      e.printStackTrace();
    }
    }//GEN-LAST:event_jbAceptarActionPerformed



    private void cargarValoresInterfaces() {
    try {
      List listaRedes = Propiedades.obtenerInfoRedes();
      Object[][] valores = new Object[listaRedes.size()][4];
      String[] columnas = { "Interface", "IP", "HOST", "ADDRESS" };
      Class[] tipos = { String.class, String.class, String.class, String.class };
      boolean[] editable = { false, false, false, false };
      for (int i = 0; i < listaRedes.size(); ++i) {
        List subLista = (List)listaRedes.get(i);
        NetworkInterface ntwi = (NetworkInterface)subLista.get(0);
        InetAddress direcc = (InetAddress)subLista.get(1);
        valores[i][0] = ntwi.getDisplayName();
        valores[i][1] = direcc.getHostAddress();
        valores[i][2] = direcc.getHostName();
        valores[i][3] = Conversiones.convierteArrayByte_HexadecimalString(direcc.getAddress());
      }

      formatearTabla(valores, columnas, tipos, editable);
    } catch (Exception e) {
      Utiles_MensajesDialogo.mensajeError("Error al recuperar la informacion de las redes\n" + e.getMessage());
      e.printStackTrace();
    }
  }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                jdInfoRedes dialog = new jdInfoRedes(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JTable jtTablaRedes;
    // End of variables declaration//GEN-END:variables

}
